package com.plian.Tools.operation;

import com.googlecode.aviator.AviatorEvaluator;

import java.math.BigDecimal;
import java.util.Map;

/**
 * 表达式运算
 */
public class OperationExpressionUtil {

    /**
     * @param operation    公式
     * @param map          参数
     * @param newScale     保留小数位数
     * @param roundingMode 保留小数方式
     * @return
     */
    public String operation(String operation, Map map, int newScale, int roundingMode) {
        try {
            BigDecimal b = (BigDecimal) AviatorEvaluator.execute(operation, map);
            return b.setScale(newScale, roundingMode).toPlainString();
        }catch (Exception e){
            return "0";
        }
    }

    public Boolean operation(String operation, Map<String, Object> valueMap){
        Object execute = AviatorEvaluator.execute(operation, valueMap);
        return (Boolean) execute;
    }

    public BigDecimal operationB(String operation, Map map, int newScale, int roundingMode) {
        try {
            BigDecimal b = (BigDecimal) AviatorEvaluator.execute(operation, map);
            return b.setScale(newScale, roundingMode);
        }catch (Exception e){
            return null;
        }
    }
}
